package com.yanqu.road.server.gameplayer.module.specialtombrob;

import com.yanqu.road.entity.activity.specialtombrob.config.SpecialTombRobConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.specialtombrob.SpecialTombRobUserData;

import com.yanqu.road.logic.bussiness.activity.SpecialTombRobBusiness;
import com.yanqu.road.pb.specialtombrob.SpecialTombRobProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.specialtombrob.SpecialTombRobMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class SpecialTombRobModule  extends GeneralModule {

    private SpecialTombRobUserData userData;

    public SpecialTombRobModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userData = SpecialTombRobBusiness.getSpecialTombRobUserData(getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userData != null){
            if (userData.isInsertOption()){
                SpecialTombRobBusiness.insertSpecialTombRobUserData(userData);
            }else if (userData.isUpdateOption()){
                SpecialTombRobBusiness.updateSpecialTombRobUserData(userData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {

    }

    @Override
    public void loginSendMsg() {
        syncConfig();
        syncUserData();
    }

    public void syncConfig(){
        if (SpecialTombRobMgr.getActivityInfo() == null) {
            return;
        }
        SpecialTombRobProto.SpecialTombSyncConfig.Builder respMsg = SpecialTombRobProto.SpecialTombSyncConfig.newBuilder();
        respMsg.setRet(0);
        player.sendPacket(ClientProtocol.U_SPECIAL_TOMB_ROB_SYNC_CONFIG, respMsg);

    }

    public void syncUserData() {
        if (SpecialTombRobMgr.getActivityInfo() == null) {
            return;
        }
        if (userData == null) {
            initUserData();
        }
        if (userData == null){
            return;
        }
        SpecialTombRobProto.SpecialTombSyncUserData.Builder respMsg = SpecialTombRobProto.SpecialTombSyncUserData.newBuilder();
        SpecialTombRobProto.SpecialTombUserData.Builder userBuilder = SpecialTombRobProto.SpecialTombUserData.newBuilder();
        userBuilder.setGuideId(userData.getGuideBit());
        respMsg.setData(userBuilder);
        player.sendPacket(ClientProtocol.U_SPECIAL_TOMB_ROB_SYNC_USER_DATA, respMsg);

    }

    public int finishGuide(List<Integer> steps) {
        if (userData == null){
            if (SpecialTombRobMgr.getActivityInfo() != null) {
                initUserData();
            }
        }
        if (userData == null) {
            return GameErrorCode.E_SPECIAL_TOMB_ROB_NOT_OPEN;
        }

        SpecialTombRobConfig config = SpecialTombRobMgr.getConfig();
        if (config == null) {
            return GameErrorCode.E_SPECIAL_TOMB_ROB_CONFIG_ERROR;
        }
        SpecialTombRobProto.SpecialTombRobFinishGuideRespMsg.Builder respMsg = SpecialTombRobProto.SpecialTombRobFinishGuideRespMsg.newBuilder();
        respMsg.setRet(0);
        Property guideReward = new Property();
        for (Integer step : steps) {
            if (isFinishGuide(step)) {
                continue;
            }
            // 判断是否有奖励
            if (step == 3){
                String configGuideReward = config.getGuideReward();
                if (!StringUtils.isNullOrEmpty(configGuideReward)){
                    if (!configGuideReward.equals("0")){
                        guideReward = PropertyHelper.parseStringToProperty(configGuideReward);
                    }
                }
            }
            // 记录引导
            userData.setGuideBit(userData.getGuideBit() | (1 << (step - 1)));
        }
        if (guideReward != null && !guideReward.isNothing()) {
            player.getModule(CurrencyModule.class).addCurrency(guideReward,eLogMoneyType.SpecialTombRob,eLogMoneyType.SpecialTombRobGuideReward);
            respMsg.setReward(PropertyHelper.parsePropertyToString(guideReward));
        }
        respMsg.setGuideBit(userData.getGuideBit());
        player.sendPacket(ClientProtocol.U_SPECIAL_TOMB_ROB_FINISH_GUIDE, respMsg);
        return 0;
    }

    /**
     * 初始化玩家数据
     */
    private void initUserData() {
        if (SpecialTombRobMgr.getActivityInfo() == null) {
            return;
        }

        if (userData != null) {
            return;
        }

        SpecialTombRobUserData tmpUserData = new SpecialTombRobUserData();
        tmpUserData.setUserId(getUserId());
        tmpUserData.setGuideBit(0);
        tmpUserData.setInsertOption();
        userData = tmpUserData;
    }


    /**
     * 是否完成引导
     */
    public boolean isFinishGuide(int step) {
        if (userData == null) {
            return false;
        }
        return ((userData.getGuideBit() >> (step - 1) & 1) == 1);
    }


}
